<?php

namespace app\index\model;

use think\Model;
use app\warehouse\model\WareHouse;
use app\shop\model\Shop as shopModel;
//use app\index\model\Attachment as AttachmentModel;
//use app\member\model\Member;

/**
 * 购物车
 */
class Cart extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xb_cart';

    //查询
    public function getCartData($member,$goodsId=array())
    {
    	$data = array();

        $mid = intval($member['id']);
        if($mid==0) return false;

        // 仓库
        $wareHouseModel = new WareHouse();
        $wareHouseList = $wareHouseModel->getAllList();
        //$whx = $wareHouseModel->getOne(1);
        //dump($whx);exit;
        //$list  = self::all(['mid'=>$mid,'goods_id'=>$goodsId]);
        $map['mid'] = $mid;
        if($goodsId) $map['goods_id'] = ['in', $goodsId];
        $list = $this->where($map)->select();
        
        foreach($list as $key=>$val)
        {
            $wid = $val['warehouse_id'];
            $shop_id = $val['shop_id'];
            $class_id = $val['class_id'];
            
            //仓库
            if( !isset($data[$wid]) ){
                $wareHouseName = isset($wareHouseList[$wid]) ? $wareHouseList[$wid] : '';
                $data[$wid] = ['warehouse_id'=>$wid,'warehouse_name'=>$wareHouseName];
            }

            //店铺
            if( !isset($data[$wid]['shop'][$shop_id]) ){
                $shopData = \think\Loader::model('app\shop\model\Shop')->getShopBaseInfo($shop_id);
                $shop_name = isset($shopData['name']) ? $shopData['name'] : '';
                $data[$wid]['shop'][$shop_id] = ['shop_id'=>$shop_id,'shop_name'=>$shop_name];
                $data[$wid]['shop'][$shop_id]['total_goods_weight'] = $val['weight'] * $val['goods_num'];
            }else{
                $data[$wid]['shop'][$shop_id]['total_goods_weight'] += $val['weight'] * $val['goods_num'];
            }

            //款类
            if( !isset($data[$wid]['shop'][$shop_id]['class'][$class_id]) ){
                $classData = \think\Loader::model('app\goods\model\GoodsClass')->getOne($class_id);
                $className = isset($classData['classname']) ? $classData['classname'] : '';
                $customcode = isset($classData['customcode']) ? $classData['customcode'] : '';
                $attachmentModel = \think\Loader::model('app\common\model\Attachment')->getOne($classData['thumb']);
                $classPic = isset($attachmentModel['path']) ? $attachmentModel['path'] : '';
                $data[$wid]['shop'][$shop_id]['class'][$class_id] = ['class_id'=>$class_id, 'class_name'=>$className, 'customcode'=>$customcode, 'classPic'=>$classPic];
            }

            $goods_id = $val['goods_id'];
            $size_id = $val['size_id'];
            $color_id = $val['color_id'];
            $goods_num = $val['goods_num'];
            
            $colorData = \think\Loader::model('app\goods\model\Color')->getOne($color_id);
            $colorName = isset($colorData['name']) ? $colorData['name'] : '';
            
            $sizeData = \think\Loader::model('app\goods\model\Size')->getOne($size_id);
            $sizeName = isset($sizeData['name']) ? $sizeData['name'] : '';

            //sku价格
            $memberModel = \think\Loader::model('app\member\model\Member');
            $memberLevel = $memberModel->getMemberLevel($member['id'],$val['shop_id']);
            //echo $memberLevel;exit;
            $priceModel = \think\Loader::model('app\goods\model\Price');
            $price = $priceModel->getGoodsPrice($goods_id,$memberLevel);
            
            $totalPrice = $goods_num*$price;
            $goodsInfo = ['id'=>$val['id'], 'goods_id'=>$goods_id, 'goods_name'=>'76000', 'size_id'=>$size_id, 'size_name'=>$sizeName, 'color_id'=>$color_id, 'color_name'=>$colorName, 'goods_num'=>$goods_num, 'price'=>$price, 'totalPrice'=>$totalPrice];
            
            //sku
            if( isset($data[$wid]['shop'][$shop_id]['class'][$class_id]['goods']) ){
                array_push($data[$wid]['shop'][$shop_id]['class'][$class_id]['goods'], $goodsInfo);
            }else{
                $data[$wid]['shop'][$shop_id]['class'][$class_id]['goods'][$goods_id] = $goodsInfo;
            }
        }
    	return $data;
    }

    //删除商品 sky
    public function delCartData($id)
    {
        $id = intval($id);
        
        if($id==0) return false;
        
        return self::destroy($id);
    }

    //计算某订单商品总重 sky
    public function getOrderGoodsWeight($mid, $warehouseId, $shopId)
    {
        $map['mid'] = $mid;
        $map['warehouse_id'] = $warehouseId;
        $map['shop_id'] = $shopId;
        $list = $this->where($map)->column('goods_id','goods_num','weight');
        $weight = 0;
        foreach($list as $v)
        {
            $weight += $v['goods_num']*$weight;
        }
        return $weight;
    }
}